{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6b76d35d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import xlwings as xw\n",
    "\n",
    "link = \"http://www.santostang.com/\"\n",
    "\n",
    "response = requests.get(\"https://movie.douban.com/top250\", headers={\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'\n",
    "})\n",
    "soup = BeautifulSoup(response.text, \"lxml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f790c5d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "itemList = soup.select(\"ol.grid_view > li > div.item\")\n",
    "dataList = []\n",
    "\n",
    "for item in itemList:\n",
    "    movieName = item.select_one(\"div.hd > a > span\").text\n",
    "    poster = item.select_one(\"img\")['src']\n",
    "    doubanScore = item.select_one(\"span.rating_num\").text\n",
    "\n",
    "    doubanScoresNumSpanList = [span.text for span in item.select(\n",
    "        \"span\") if \"人评价\" in span.text]\n",
    "\n",
    "    if(len(doubanScoresNumSpanList) > 0):\n",
    "        doubanScoresNum = doubanScoresNumSpanList[0][:-3]\n",
    "    doubanQuote = item.select_one(\"p.quote > span.inq\").text\n",
    "\n",
    "    data = {\n",
    "        \"movieName\": movieName,  # 电影名\n",
    "        \"poster\": poster,  # 电影海报\n",
    "        \"doubanScore\": doubanScore,  # 豆瓣分数\n",
    "        \"doubanScoresNum\": doubanScoresNum,  # 豆瓣评分数\n",
    "        \"doubanQuote\": doubanQuote  # 豆瓣引述\n",
    "    }\n",
    "\n",
    "    dataList.append(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ccefa6c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "sheetDataList = [['电影名', '电影海报', '豆瓣分数', '豆瓣评分数', '豆瓣引述']]\n",
    "\n",
    "for data in dataList:\n",
    "    sheetDataList.append([data['movieName'], data['poster'],\n",
    "                         data['doubanScore'], data['doubanScoresNum'], data['doubanQuote']])\n",
    "\n",
    "app = xw.App(visible=False, add_book=False)\n",
    "workbook = app.books.add()\n",
    "sheet = workbook.sheets[0]\n",
    "\n",
    "sheet.range('A1').options(expand='table').value = sheetDataList\n",
    "\n",
    "workbook.save(\"豆瓣Top250.xlsx\")\n",
    "app.quit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
